package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddCache;
import com.baizhi.annotation.AddLog;
import com.baizhi.annotation.DelCache;
import com.baizhi.dao.AdminMapper;
import com.baizhi.entity.Admin;
import com.baizhi.entity.AdminExample;
import com.baizhi.service.AdminService;
import com.baizhi.util.Md5Utils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service("adminService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;

    @Resource
    HttpSession session;

    @Override
    public HashMap<String, Object> login(Admin admin, String clientCode) {
        HashMap<String, Object> map = new HashMap<>();
        //对比验证码是否正确
        //获取serverCode
        String serverCode = ((String)session.getAttribute("serverCode")).toLowerCase();
        System.out.println(" s "+serverCode);
        System.out.println(clientCode);
        if(serverCode.equals(clientCode.toLowerCase())){
            //验证码正确
            Admin login = adminMapper.queryByUsername(admin.getUsername());
            //判断管理员是否存在
            if(login!=null){
                //判断管理员状态是否可登录
                if(login.getStatus()==1){
                    //该管理员属于正常状态
                    //判断密码是否正确
                    if(login.getPassword().equals(admin.getPassword())){
                        //登录成功
                        session.setAttribute("admin",login);

                        map.put("status","100");
                        map.put("message","登录成功");
                    }else{
                        map.put("status","101");
                        map.put("message","用户密码不正确");
                    }
                }else{
                    //被限制登录
                    map.put("status","101");
                    map.put("message","该用户被限制登录");
                }

            }else{
                //管理员不存在
                map.put("status","101");
                map.put("message","该用户不存在");
            }
        }else{
            //验证码错误
            map.put("status","101");
            map.put("message","验证码不正确");
        }
        return map;
    }

    @AddCache
    @Override
    public HashMap<String, Object> pageAllAdmin(Integer page, Integer rows) {
        HashMap<String, Object> map = new HashMap<>();
        //创建admin的条件查询条件实体
        AdminExample example = new AdminExample();
        map.put("page",page);
        //获取行数
        int records = adminMapper.selectCountByExample(example);
        map.put("records",records);
        //总页数
        int total = records%rows==0?records/rows:records/rows+1;
        map.put("total",total);
        //分页数据
        RowBounds rowBounds = new RowBounds((page-1)*rows,rows);
        List<Admin> admins = adminMapper.selectByExampleAndRowBounds(example, rowBounds);
        map.put("rows",admins);
        return map;
    }

    @DelCache
    @AddLog(description = "修改管理员")
    @Override
    @Transactional
    public void update(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @DelCache
    @AddLog(description = "添加管理员")
    @Override
    @Transactional
    public String add(Admin admin) {
        admin.setId(UUID.randomUUID().toString());
        admin.setSalt(Md5Utils.getSalt(6));
        admin.setStatus(1);
        adminMapper.insert(admin);
        return admin.getId();
    }

    @DelCache
    @AddLog(description = "删除管理员")
    @Override
    @Transactional
    public void del(Admin admin) {
        adminMapper.delete(admin);
    }
}
